var applyEditApp = applyEditApp || {};
applyEditApp.model = applyEditApp.model || {};
applyEditApp.view = applyEditApp.view || {};

$(function () {

    $("#pathUp").text('物业申请');
    $("#pathDown").text('申请表详情');

    applyEditApp.model.applyModel = Backbone.Model.extend({
        url: window.ctx + "/rest/apply/get",
        defaults: {
            id: '',
            applyName: '',
            applyTel: '',
            applyTime: '',
            required: '',
            handleNum: '',
            handleResult: '',
            handleRemark: '',
            handleTime: '',
            responsible: '',
        },
    });
    applyEditApp.view.executeView = Backbone.View.extend({
        el: "#formDiv",
        model: new applyEditApp.model.applyModel(),

        template: _.template($("#applyTem").html()),

        events: {
            "click #saveBtn": "save",
            "click #backBtn": "back",
        },
        initialize: function () {
            var context = this;
            this.model.fetch({
                data: {
                    id: $("form").find("[name=id]").val()
                },
                success: function () {
                    context.render();
                },
                error: function () {
                    hintDialog("服务器出错！");
                },
            });
        },
        render: function () {
            $(this.el).find("form").append(this.template(this.model.toJSON()));
            $(":text:eq(0)").focus();
        },
        save: function () {
            var context = this;
            var validate = $(context.el).find("form").validate({
                debuge: false,
                rules: {
                    applyName: {
                        required: true,
                        remote: {
                            url: window.ctx + "/rest/owner/checkOwnerIsTrue",
                            type: "post",
                            dataType: "json",
                            data: {
                                applyName: function () {
                                    return $("[name=applyName]").val();
                                },
                            },

                        }
                    },
                    applyTel: {
                        required: true,
                        isPhone: true,
                    },
                    required: {
                        stringMinLength: 20,
                        required: true,
                    }
                },
                messages: {
                    applyName: {
                        required: "申请人姓名不能为空",
                        remote:"请正确填写登记的业主姓名",
                    },
                    applyTel: {
                        required: "联系电话不能为空",
                    },
                    required: {
                        required: "申请事宜不能为空",
                    }
                },
                submitHandler: function () {
                    var param = {id: $("[name=id]").val()};
                    param["applyName"] = $("[name=applyName]").val();
                    param["applyTel"] = $("[name=applyTel]").val();
                    param["required"] = $("[name=required]").val();
                    param["handleResult"] = $("[name=handleResult]").val();
                    param["handleRemark"] = $("[name=handleRemark]").val();
                    $.ajax({
                        url: window.ctx + "/rest/apply/save",
                        dataType: "json",
                        type: "post",
                        contentType: "application/json",
                        data: JSON.stringify(param),
                        success: function (data) {
                            if (data) {
                                $.dialog.alert("信息增加成功", function () {
                                    location.href = window.ctx + "/apply/list";
                                })
                            } else {
                                console.dir(data);
                                hintDialog("保存申请信息失败！");

                            }
                        },
                        error: function (data) {
                            hintDialog("服务器出错！");
                        }
                    });
                    return;
                },
            });
            $(context.el).find("form").submit();
        },
        back: function () {
            location.href = window.ctx + "/apply/list";
        }
    });
    new applyEditApp.view.executeView();

})